#include "BoxBlurFilter.h"

BoxBlurFilter::BoxBlurFilter(void) : boxDimension(0)
{
}
BoxBlurFilter::BoxBlurFilter(int pBoxDimension) : boxDimension(pBoxDimension)
{
}
BoxBlurFilter::~BoxBlurFilter(void)
{
}

bool BoxBlurFilter::apply(Image& pImage)
{
	int h = pImage.getHeight();
	int w = pImage.getWidth();

	Image bluredImage = Image(w, h);

	for(int i = 0; i< h; i++)
	{
		for(int j = 0; j< w; j++)
		{
			Image::Color averagePixel = {0.0f, 0.0f, 0.0f};

			int u = i-(boxDimension-1)/2;
			int v = j-(boxDimension-1)/2;
			
			int total = 0.0;
			for(int k =u; k < u+boxDimension; k++)
			{
				for(int l = v; l< v+boxDimension; l++)
				{
					if( k >=0 && l >=0 && l < w && k < h)
					{
						Image::Color adjtColor = pImage.getValue(l, k);
						averagePixel.r += adjtColor.r;
						averagePixel.g += adjtColor.g;
						averagePixel.b += adjtColor.b;
						total++;
					}
					
				}
			}

			averagePixel.r = averagePixel.r/ total;
			averagePixel.g = averagePixel.g/ total;
			averagePixel.b = averagePixel.b/ total;

			bluredImage.setValue(j, i, averagePixel);
		}
	}

	pImage = bluredImage;

	return true;
}


void BoxBlurFilter::setBoxDimension(int pBoxDimension)
{
	boxDimension = pBoxDimension;
}
int BoxBlurFilter::getBoxDimension()
{
	return boxDimension;
}